package application.services;

import application.exceptions.ItemNotFoundException;
import application.model.SignaturesModel;
import application.model.modify.SignatureModel;
import application.translators.Translator;
import application.translators.impl.SignaturesTranslator;
import dao.SignatureDao;
import mappings.Signature;
import org.hibernate.exception.ConstraintViolationException;

import javax.ws.rs.*;
import javax.ws.rs.core.MediaType;
import java.util.List;

@Path("signatures")
public class SignaturesService
{
    private SignatureDao signatureDao = new SignatureDao();
    private Translator<List<Signature>, SignaturesModel> translator = new SignaturesTranslator();

    @Path("getForMaterial/{materialId}")
    @GET
    @Produces(MediaType.APPLICATION_XML)
    public SignaturesModel getSignaturesForMaterial(@PathParam("materialId") int materialId)
    {
        return translator.translate(signatureDao.getSignaturesForMaterial(materialId));
    }

    @Path("addSignature")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public SignatureModel addSignature(@FormParam("materialId") int materialId,
                                                           @FormParam("countryId") int countryId,
                                                           @FormParam("name") String name)
    {
        SignatureModel signatureModel = new SignatureModel();

        try
        {
            Signature signature = signatureDao.addSignature(materialId, countryId, name);
            signatureModel.setId(signature.getId());
            signatureModel.setName(name);
            signatureModel.setCountryName(signature.getCountry().getName());
            signatureModel.setStatus(0);
        }
        catch (ConstraintViolationException e)
        {
            // lapiemy wyjatek rzucany przez hibernate'a dla unique constraint - ustawiamy status 1 (rekord istnieje)
            signatureModel.setStatus(1);
        }
        catch (Exception e)
        {
            // lapiemy wszystkie inne exceptiony i ustawiamy status na 2 (czyli cos sie wyjebalo, ale nie wiadomo co :P)
            signatureModel.setStatus(2);
        }

        return signatureModel;
    }

    @Path("editSignature")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public SignatureModel editSignature(@FormParam("id") Integer id,
                                                  @FormParam("materialId") int materialId,
                                                  @FormParam("countryId") int countryId,
                                                  @FormParam("name") String name)
    {
        SignatureModel signatureModel = new SignatureModel();

        try
        {
            Signature signature = signatureDao.editSignature(id, materialId, countryId, name);
            signatureModel.setName(name);
            signatureModel.setCountryName(signature.getCountry().getName());
            signatureModel.setId(signature.getId());
            signatureModel.setStatus(0);
        }
        catch (ItemNotFoundException e)
        {
            signatureModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            signatureModel.setStatus(2);
        }

        return signatureModel;
    }

    @Path("deleteSignature")
    @POST
    @Produces(MediaType.APPLICATION_XML)
    public SignatureModel deleteSignature(@FormParam("id") Integer id)
    {
        SignatureModel signatureModel = new SignatureModel();

        try
        {
            signatureDao.deleteSignature(id);
            signatureModel.setStatus(0);
        }
        catch (ItemNotFoundException e)
        {
            signatureModel.setStatus(1); // 1 = nie znaleziono rekordu o danym id
        }
        catch (Exception e)
        {
            signatureModel.setStatus(2);
        }

        return signatureModel;
    }
}
